/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Common protocol header for all GIMnetAP (tcpHub) protocols.
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _GIMNETAP_PROTOCOLID_H_
#define _GIMNETAP_PROTOCOLID_H_

#define PROTOCOL_FLAGBIT(bit) (1<<(bit))
#define PROCOTOL_GETPAYLOADSIZE(apid, hdrsize) (apid->total_size - hdrsize)
#define PROTOCOL_TOTAL2PAYLOAD(p_total_size) \
        ((p_total_size) - sizeof(TProtocolIdentification))
#define PROTOCOL_PAYLOAD2TOTAL(p_payload_size) \
        ((p_payload_size) + sizeof(TProtocolIdentification))


// Maximum number of protocols 
#define MAX_PROTOCOL_COUNT 256


/**
 * This is the compulsory header for every message sent to and from
 * the GIMnetAP. 
 *
 * It contains the total_size of the datagram. This total size
 * includes the size of the total_size field itself.
 *
 * protocol_number is the registered protocol number. This number
 * dictates which protocol handler the incoming message is handed over
 * into.
 */
typedef struct _protocolIdentification
{
  unsigned total_size         : 28;      ///< Datagram total size (Including this)
  unsigned protocol_number    : 4;       ///< Protocol number

} TProtocolIdentification;
#define SIZEOF_TPROTOCOLIDENTIFICATION (4)

void protocolIdentificationConstructHeader(TProtocolIdentification *p,
					   int protocol_number,
					   int total_size);

void protocolIdentificationDumpHeader(int l, const TProtocolIdentification *protocol_id);


#endif // _GIMNETAP_PROTOCOLID_H_
